<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1、2、3、4、test、888、before timeout、alse before timeout、444
        setTimeout(() => {
            // #1
            new Promise(resolve => {
                resolve();
            }).then(() => {
                // #4
                console.log('test');
            });

            console.log(4);
            setTimeout(function () {
                // #5
                Promise.resolve().then(res => {
                    // #10
                    console.log(444);
                })
            })
            Promise.resolve().then(res => {
                // #6
                console.log(888);
            })
        });

        new Promise(resolve => {
            resolve();
            console.log(1)
        }).then(() => {
            // #2
            console.log(3);
            setTimeout(function () {
                // #3
                Promise.resolve().then(() => {
                    // #7：这一行确确实实打印了 before timeout，但是执行完毕后又产生了一个微任务
                    console.log('before timeout');
                    // return Promise.resolve(undefined)
                }).then(() => {
                    // #8
                    Promise.resolve().then(() => {
                        // #9
                        console.log('also before timeout')
                    })
                })
            })
        })
        console.log(2);
    </script>
</body>

</html>